/*
 * @Date: 2023-02-15 15:55:30
 * @LastEditTime: 2023-03-11 16:19:42
 * @FilePath: /xing-qiu-da-zhan/src/api/index.ts
 * 介绍:
 */
import type { AxiosRequestConfig } from "axios";
import Request, { RequestConfig } from "@@/utils/tools/Request";
import useSysStore from "@@/store/useSysStore";

const env = import.meta.env;

const defaHttp: InstanceType<typeof Request> = new Request();
if (env.VITE_PROXY == "1") {
  defaHttp.axios.defaults.baseURL = env.VITE_API_PREFIX; //配置默认请求地址--跨域代理
} else {
  defaHttp.axios.defaults.baseURL = env.VITE_BASE_URL + env.VITE_API_PREFIX; //配置默认请求地址--无代理
}
defaHttp.axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded";
defaHttp.axios.interceptors.request.use(<any>((
  config: RequestConfig
): AxiosRequestConfig => {
  const sysStore = useSysStore();
  /**token */
  const token = sysStore.tokenInfo?.access_token;
  let headers: AnyObj = config.headers || {};
  if (sysStore.reqHeader) headers = { ...headers, ...sysStore.reqHeader };
  return { ...config, headers };
}));
export { defaHttp };

/**请求res类型 */
export interface ApiRes<T> {
  code: 200;
  msg: string;
  status: boolean;
  data: T;
}
export namespace Paging {
  /**请求分页接口返res类型 */
  export type Res<T> = ApiRes<Data<T>>;
  /**分页data类型 */
  export interface Data<T> {
    /**当前页码数 */
    current_page: number;
    data: T;
    /**第一页URL */
    first_page_url: string;
    /**首页 */
    from: number;
    /**尾页 */
    last_page: number;
    /**尾页URL */
    last_page_url: string;
    links: Array<{
      url: string | null;
      label: string;
      active: boolean;
    }>;
    /**下一页url */
    next_page_url: string;
    path: string;
    /**每页数据长度 */
    per_page: number;
    prev_page_url: string;
    /**下一页 */
    to: number;
    /**总数 */
    total: number;
  }

  /**分页参数 */
  export interface Req {
    page: number;
  }
}
